<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="loginDiv">
        <div class="title-container">
          <h3 class="title">
            <img
              class="logo-img"
              src="@/assets/logo/logo-xx.png"
              alt="logo"
              width="102"
              height="27"
            >
          </h3>
          <div class="tips">
            <span>Automatic Operation Maintenance System</span>
          </div>
        </div>
        <el-form-item prop="username">
          <span class="svg-container">
            <font-icon icon-class="icon-yonghu" />
          </span>
          <el-input
            ref="username"
            v-model="loginForm.username"
            placeholder="用户名"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>
        <el-form-item prop="password">
          <span class="svg-container">
            <font-icon icon-class="icon-suoding" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="密码"
            name="password"
            tabindex="2"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon
              :icon-class="passwordType === 'password' ? 'icon-biyanjing' : 'icon-yanjing'"
            />
          </span>
        </el-form-item>
        <div style="margin-bottom: 15px; height: 52px; line-height: 50px">
          <el-checkbox v-model="keepPassword">记住密码</el-checkbox>
        </div>
        <el-button
          :loading="loading"
          type="primary"
          size="medium"
          style="width: 100%; margin-bottom: 15px"
          @click.native.prevent="handleLogin"
        >登录</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
// import { validUsername } from '@/utils/validate'
import { Test2 } from '@/api/user'
import CryptoJS from 'crypto-js'
import { ParamArr } from '../../../public/js/paramarr'
import Global from '../../../public/js/global'
import CreateSocket from '../../../public/js/webSocket'
// import Vue from 'vue'
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入用户名。'))
      } else {
        callback()
      }
      // if (!validUsername(value)) {
      //   callback(new Error('请输入用户名。'))
      // } else {
      //   callback()
      // }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length === 0) {
        callback(new Error('密码不能为空。'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: 'admin',
        password: '1'
      },
      loginRules: {
        username: [
          {
            required: true,
            trigger: 'blur',
            validator: validateUsername
          }
        ],
        password: [
          {
            required: true,
            trigger: 'blur',
            validator: validatePassword
          }
        ]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      deskey: '',
      keepPassword: false
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    Test2(new ParamArr()).then((vPar) => {
      // 制品库：sysLogoIndex = 57
      sessionStorage.setItem('sysLogoIndex', vPar.getStr('SysLogoIndex'))
      sessionStorage.setItem('g_isRPA', vPar.getBool('RPA')) // 当前环境
      sessionStorage.setItem('g_systemConfig', JSON.stringify(new Map()))
      if (vPar.getBool('RPA') || vPar.getStr('SysLogoIndex') === '60') {
        sessionStorage.setItem('isRPALogo', true)
      }
      this.deskey = vPar.getStr('key')
      // 保存后台是否使用了utf8编码
      sessionStorage.setItem('isEncodingUTF8', vPar.getBool('EncodingUTF8'))
    })
  },
  mounted() {
    window.CreateSocket = CreateSocket
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          var md5Str = CryptoJS.MD5(this.loginForm.password)
            .toString(CryptoJS.enc.Hex)
            .toUpperCase()
          var vPar = new ParamArr()
          vPar.setStr('User', this.loginForm.username)
          vPar.setStr('Pass', Global.encryptByDES(md5Str, this.deskey))
          vPar.setStr(
            'PassDES',
            Global.encryptByDES(this.loginForm.password, this.deskey)
          )
          vPar.setStr('Key', this.deskey)
          vPar.setBool('webJson', true)
          this.$store
            .dispatch('user/login', vPar)
            .then(() => {
              // 获取系统权限之后才能进入到首页
              Global.getSystemConfig().then(res => {
                this.$router.push({
                  path: this.redirect || '/',
                  query: this.otherQuery
                })
                this.$message.closeAll()
                this.loading = false
                this.$message.success('登录成功!')
              }).catch(err => {
                console.log(err)
              })
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
$bg: #fff;
$light_gray: #000;
$cursor: #000;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 40px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(0, 0, 0, 0.2);
    background: rgba(255, 255, 255, 0.1);
    border-radius: 5px;
    color: #454545;
  }

  .loginDiv {
    border-top: 3px solid #4285f4;
    border-radius: 3px;
    background-color: #fff;
    padding: 30px;
  }
}
</style>

<style lang="scss" scoped>
$bg: #eee;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  position: relative;

  .login-form {
    width: 430px;
    height: 70%;
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    margin: auto;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      text-align: center;
      display: block;
      line-height: 20px;
      font-size: 13px;
      font-weight: 100;
      color: #5a5a5a;
      margin-top: 11px;
      padding-bottom: 10px;
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;
    margin-bottom: 22px;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0 auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
